from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
import json
import time
import multiprocessing
import random

class Body():
    def __init__(self):
        self.options = webdriver.ChromeOptions()
        self.options.add_argument('disable-infobars')
        self.options.add_experimental_option("excludeSwitches", ['enable-automation'])
        self.options.add_argument('--disable-blink-features')
        self.options.add_argument('--disable-blink-features=AutomationControlled')
        self.options.add_argument('--disable-gpu')
        #chromedriver路径
        service = Service('C:/Program Files/Google/Chrome/Application/chromedriver.exe')
        self.browser = webdriver.Chrome(service=service, options=self.options)
        self.browser.maximize_window()
        self.browser.implicitly_wait(5)
        self.action_chains = ActionChains(self.browser)

    def do(self,ls,d,i):
        self.browser.get(ls[0])
        driver_wait = WebDriverWait(self.browser, 10)
        for j in range(120):
        # for target in ls:
            time.sleep(random.randint(5,10))
            target=ls[random.randint(0,4)]
            newTab=f'window.location.replace("{target}")'
            # st=time.time()
            self.browser.execute_script(newTab)
            # et=time.time()
            # with open(f'test{i}.txt','a') as f:
            #     f.write(f'{d[target]}{et-st} ')
        # input()

def one_query(inter,d,i):
    b = Body()
    b.do(inter,d,i)

if __name__ == '__main__':
    # 1. 采集响应延迟 
    site_http_name='http://10.129.19.79:5000'
    sites=[f'{site_http_name}/get_bt_tasks',f'{site_http_name}',f'{site_http_name}/bt_task',f'{site_http_name}/datasubsets/4',f'{site_http_name}/bttask_exec_history']
    labels=list('abcde')
    d={}
    for i in range(len(sites)):
        d[sites[i]]=labels[i]
    # for i in range(20):
    #     one_query(sites,d,10)
    processes=[]
    for i in range(10):
        p=multiprocessing.Process(target=one_query,args=(sites,d,i))
        processes.append(p)
        print(i)
    for p in processes:
        p.start()
    for p in processes:
        p.join()

    # 2. 计算响应延迟的平均值
    # statistics={'a':[],'b':[],'c':[],'d':[],'e':[]}
    # for i in range(1):
    #     with open(f'test10.txt','r') as f:
    #         contents=f.read()
    #     for line in contents.strip().split('\n'):
    #         words=line.strip().split(' ')
    #         for word in words:
    #             statistics[word[0]].append(float(word[1:]))
    # for k in statistics:
    #     print(k,sum(statistics[k])/len(statistics[k]))

    # 3. 计算数据库交互延迟的平均值
    # with open('test.txt','r') as f:
    #     contents=f.read()
    # a=[]
    # b=[]
    # c=[]
    # d=[]
    # e=[]
    # for line in contents.strip().split('\n'):
    #     words=line.strip().split(' ')
    #     for word in words:
    #         if word[0]=='a':
    #             a.append(float(word[1:]))
    #         elif word[0]=='b':
    #             b.append(float(word[1:]))
    #         elif word[0]=='c':
    #             c.append(float(word[1:]))
    #         elif word[0]=='d':
    #             d.append(float(word[1:]))
    #         elif word[0]=='e':
    #             e.append(float(word[1:]))
    # for i in [a,b,c,d,e]:
    #     print(sum(i)/len(i))